<template>
  <div class="marquee-box">
    <div ref="track" class="marquee-track">
      <div ref="list" class="marquee-list">
        <slot></slot>
      </div>
      <div v-html="clonedHTML" class="marquee-list">
      </div>
    </div>
  </div>
</template>

<script>
  const stepNext = (boundary, step = 1) => {
    let tok = 0;
    const floor = Math.floor(boundary);
    const fraction = boundary - floor;
    return function() {
      return fraction + (tok += step) % floor;
    };
  };
  export default {
    data() {
      return {
        clonedHTML: '',
      };
    },
    mounted() {
      this.clonedHTML = this.$refs.list.innerHTML;
      window.addEventListener('resize', this.reset.bind(this), false);
      this.$nextTick(() => {
        this.reset();
        requestAnimationFrame(this.animate);
      });
    },
    methods: {
      animate() {
        requestAnimationFrame(this.animate);
        if (this.$refs.track) {
          this.$refs.track.style.left = `-${this.step()}px`;
        }
      },
      reset() {
        this.boundary = this.$refs.list.getBoundingClientRect().width;
        this.step = stepNext(this.boundary);
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "~common/style/variables";
  .marquee-box {
    overflow: hidden;
    position: absolute;
    z-index: 1;
    width: 100%;
    height: $marquee-box-height;
    line-height: $marquee-box-height;
    color: $marquee-box-color;
    background-color: $marquee-box-bg-color;
  }
  .marquee-track {
    position: absolute;
    white-space: nowrap;
    font-size: $fs-0;
  }
  .marquee-list {
    font-size: $fs-36;
    display: inline-block;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    &:not(:last-child) {
      padding-right: $marquee-box-marquee-spacing;
    }
  }
</style>
